package com.adjy.pathbuilders.screens;

import static com.adjy.pathbuilders.commons.Preconditions.check;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.view.MotionEvent;

import com.adjy.pathbuilders.R;
import com.adjy.pathbuilders.commons.FontLoader;
import com.adjy.pathbuilders.commons.FontLoader.Fonts;
import com.adjy.pathbuilders.rendering.GameCanvas;
import com.adjy.pathbuilders.state.Game;

public class Counter implements GameScreen {
	
	private int current;
	private int limit;
	private Paint textPaint;
	private Paint stroke;
	private Game game;
	
	private int fontSize;
	private float effectTime;	
	private boolean inSpecialEffect;
	private boolean showNumber;
	private int showNumberCount;
	
	private static final int effectTimeOut = 100; 
	private static final int showNumberThreshold = 5;
	
	public Counter(Game game, int initialLimit, int initialCurrent) {
		check(initialLimit >= 0, "initialLimit must not be negative.");
		check(initialCurrent >= 0, "initialCurrent must not be negative.");
		
		this.game = game;
		
		limit = initialLimit;
		current = initialCurrent;
		
		fontSize = 20;
		inSpecialEffect = false;
		showNumberCount = 0;
		showNumber = true;
		effectTime = 0;
		
		textPaint = textPaint();
		stroke = stroke();
	}

	private Paint textPaint() {
		Paint textPaint = new Paint();
		textPaint.setAntiAlias(true);
		textPaint.setTextSize(fontSize);
		textPaint.setARGB(255, 172, 51, 22);
		textPaint.setFakeBoldText(true);
		textPaint.setTypeface(FontLoader.getInstance().getFont(Fonts.HOBO));
		return textPaint;
	}
	
	private Paint stroke() {
		Paint stroke = new Paint();
		stroke.setStyle(Style.STROKE);
		stroke.setStrokeWidth(1);
		stroke.setTextSize(fontSize);
		stroke.setColor(Color.BLACK);
		stroke.setTypeface(FontLoader.getInstance().getFont(Fonts.HOBO));
		return stroke;
	}
	
	public void incrementLimit() {
		limit++;
	}
	
	public void decrementLimit() {
		limit--;
	}
	
	public void incrementCurrent() throws Exception {
		if (current >= limit) {
			throw new Exception("Limit reached");			
		}
		current++;
		inSpecialEffect = true;
	}
	
	public void decrementCurrent() throws Exception {
		if (current <= 0) {
			throw new Exception("Zero reached");
		}
		current--;
		inSpecialEffect = true;
	}

	@Override
	public void load() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void release() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void input(MotionEvent event) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void render(GameCanvas mcanvas, float timeElapsed) {
		mcanvas.drawTextWithConversion(game.getContext().getString(R.string.selectable_objects), 310, 30, textPaint);
		mcanvas.drawTextWithConversion(game.getContext().getString(R.string.selectable_objects), 310, 30, stroke);
		
		if (inSpecialEffect) {
			effectTime += timeElapsed;
			
			showNumberCount += 1;
			
			if (showNumberCount > showNumberThreshold) {
				showNumberCount = 0;
				showNumber = !showNumber;
			}
			
			if (effectTime > effectTimeOut) {
				effectTime = 0;
				inSpecialEffect = false;
			}
		}
		
		if (showNumber) {
			mcanvas.drawTextWithConversion(String.valueOf(current), 350, 50, textPaint);
			mcanvas.drawTextWithConversion(String.valueOf(current), 350, 50, stroke);
			
			mcanvas.drawTextWithConversion("/", 380, 50, textPaint);
			mcanvas.drawTextWithConversion("/", 380, 50, stroke);
			
			mcanvas.drawTextWithConversion(String.valueOf(limit), 400, 50, textPaint);
			mcanvas.drawTextWithConversion(String.valueOf(limit), 400, 50, stroke);			
		}
	}

	@Override
	public void step(float timePassed) {
		// TODO Auto-generated method stub
		
	}
}
